package com.nursebetty.server.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.List;
import java.util.TimeZone;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import com.nursebetty.server.common.UserProfile;
import com.nursebetty.server.dao.GlucossDao;
import com.nursebetty.server.model.Glucoss;

public class GlucossDaoImpl implements GlucossDao {
	private JdbcTemplate jdbcTemplate;
	
	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}


	public List<Glucoss> getResults(UserProfile user) {
		String sql = "select * from GLUCOSE_ENTRY where USR_ID=" + user.getId();
		List<Glucoss> result = jdbcTemplate.query(sql, new GlucossMapper());
		return result;
	}

	public String saveResults(Glucoss glucoss, UserProfile user, String Result) {
		// TODO Auto-generated method stub
		return null;
	}

	public void saveGlucoss(UserProfile user, Glucoss glucoss) {
		String sql="insert into GLUCOSE_ENTRY(USR_ID,GLUCOSE_LEVEL_ENTRY,GLUCOSE_RESULTS) values (?,?,?) ";
		jdbcTemplate.update(sql, new Object[] {user.getId(),glucoss.getGlucossValue(), glucoss.getGlucossValue() });

	}
	
	private class GlucossMapper implements RowMapper {

		public Glucoss mapRow(ResultSet rs, int rowNum) throws SQLException {
			Glucoss gc = new Glucoss();
			gc.setGlucossValue(rs.getInt("GLUCOSE_RESULTS"));
			Timestamp td = rs.getTimestamp("DATE_ENTRY", Calendar.getInstance(TimeZone.getTimeZone("UTC")));
			gc.setCurrentTrackingDate(td);
			return gc;
		}
	}


}
